<template>
	<xn-form-container
		:title="'质安环审核'"
		:width="1200"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-divider>上报信息</a-divider>
			<a-row :gutter="16">
				<a-col :span="6">
					<a-form-item label="上报人姓名：" name="userName">
						<a-input v-model:value="formData.userName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="上报人电话：" name="userPhone">
						<a-input v-model:value="formData.userPhone" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患类型" name="hiddenType">
						<a-select v-model:value="formData.hiddenType" :options="hiddenTypeOptions" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患上报位置：" name="address">
						<a-input v-model:value="formData.address" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="隐患描述：" name="hiddenRemark">
						<a-textarea v-model:value="formData.hiddenRemark" :rows="4" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="图片附件" name="fliePath">
						<SingPicUpload ref="fliePathUploadRef" :limitImage="0"></SingPicUpload>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="视频文件：" name="videoPath">
						<UploadFile ref="videoPathUploadRef" :limitImage="0"></UploadFile>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="声音文件：" name="soundPath">
						<UploadFile ref="soundPathUploadRef" :limitImage="0"></UploadFile>
					</a-form-item>
				</a-col>
			</a-row>
			<a-divider>核实信息</a-divider>
			<a-row :gutter="16">
				<a-col :span="6">
					<a-form-item label="核实人：" name="verifyUserName">
						<a-input v-model:value="formData.verifyUserName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="核实结果：" name="verifyResult">
						<a-input v-model:value="formData.verifyResult" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="隐患具体地址：" name="hiddenAddr">
						<a-input v-model:value="formData.hiddenAddr" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="核实情况说明：" name="verifySituation">
						<a-textarea v-model:value="formData.verifySituation" :rows="4" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="核实照片" name="verifyImage">
						<SingPicUpload ref="verifyImageUploadRef" :limitImage="0"></SingPicUpload>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="建议防范措施：" name="suggestionMeasures">
						<a-textarea v-model:value="formData.suggestionMeasures" :rows="4" disabled readonly="true"/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-divider>定级信息</a-divider>
			<a-row :gutter="16">
				<a-col :span="6">
					<a-form-item label="隐患等级：" name="hiddenLevel">
						<a-select v-model:value="formData.hiddenLevel" :options="hiddenLevelOptions" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="建议奖励金额：" name="suggestionRewards">
						<a-input-number style="width:100%" v-model:value="formData.suggestionRewards"
										disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="业务部门：" name="rectificationOrgId">
						<a-input v-model:value="formData.rectificationOrgName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="整改期限：" name="rectificationPeriod">
						<a-input v-model:value="formData.rectificationPeriod" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="分管领导：" name="chargeLeaderId">
						<a-input v-model:value="formData.chargeLeaderName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="主要领导：" name="mainLeaderId">
						<a-input v-model:value="formData.mainLeaderName" disabled readonly="true"/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-divider>审核记录</a-divider>
			<s-table
				ref="table"
				:columns="columns"
				:data="loadData"
				bordered
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'auditStatus'">
						{{ $TOOL.dictTypeData('auditResult', record.auditStatus) }}
					</template>
				</template>
			</s-table>
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="审核结果：" name="auditResult">
						<a-radio-group v-model:value="formData.auditResult">
							<a-radio :value="0">不通过</a-radio>
							<a-radio :value="1">通过</a-radio>
						</a-radio-group>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="审核备注：" name="auditRemark">
						<a-textarea v-model:value="formData.auditRemark" placeholder="请输入审核备注" :rows="4"
									allow-clear :maxlength="200"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="hiddenReportForm">
	import {cloneDeep} from 'lodash-es'
	import {required} from '@/utils/formRules'
	import hiddenReportApi from '@/api/biz/hidden/hiddenReportApi'
	import tool from "@/utils/tool"
	import SingPicUpload from '@/components/SingPicUpload/index.vue'
	import UploadFile from '@/components/FileUpload/index.vue'

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({successful: null})
	const formRef = ref()
	let searchFormState = reactive({})
	const fliePathUploadRef = ref()
	const videoPathUploadRef = ref()
	const soundPathUploadRef = ref()
	const verifyImageUploadRef = ref()

	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const hiddenTypeOptions = tool.dictList('hiddenType')
	const hiddenLevelOptions = tool.dictList('hiddenLevel')
	const table = ref()
	const columns = [
		{
			title: '流程名称',
			dataIndex: 'recordName',
			width: 160,
			align:'center'
		},
		{
			title: '审核时间',
			dataIndex: 'createTime',
			width: 160,
			align:'center'
		},
		{
			title: '操作人名称',
			dataIndex: 'createUserName',
			width: 160,
			align:'center'
		},
		{
			title: '审核结果',
			dataIndex: 'auditStatus',
			align:'center'
		},
		{
			title: '下一流程人名称',
			dataIndex: 'nextUserName',
			width: 160,
			align:'center'
		},
		{
			title: '备注',
			dataIndex: 'remark',
			align:'center'
		},
	]
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			searchFormState.reportId = record.reportId
		}
		nextTick(() => {
			if (record && record.fliePath) {
				fliePathUploadRef.value.initFileList(record.fliePath)
			}
			if (record && record.videoPath) {
				videoPathUploadRef.value.initFileList(record.videoPath)
			}
			if (record && record.soundPath) {
				soundPathUploadRef.value.initFileList(record.soundPath)
			}
			if (record && record.verifyImage) {
				verifyImageUploadRef.value.initFileList(record.verifyImage)
			}
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 审核日志
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return hiddenReportApi.pageAuditRecord(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 默认要校验的
	const formRules = {
		auditResult: [required('请选择审核结果')],
		auditRemark: [required('请输入审核备注')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			hiddenReportApi.safetyAudit(formDataParam)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
